<template>
  <div class="login-page">
    <div class="login-page-header">
      <div class="login-page-header-logo">
        <img :src="logoHeader" />
      </div>
      <div class="login-page-header-text">
        <p>{{$t("login.title")}}</p>
        <p>Kylin System Unified Manager V1.0</p>
      </div>
    </div>
    <div class="login-page-content">
      <div class="login-page-content-logo">
        <img :src="logoContent" />
      </div>
      <div class="login-page-content-form">
        <div class="login-page-content-form-header">
          <h1>{{$t("login.welcome")}}</h1>
        </div>
        <el-form
          autoComplete="on"
          :model="loginForm"
          :rules="loginRules"
          ref="loginForm"
          label-position="left"
        >
          <el-form-item prop="username">
            <el-input
              name="username"
              type="text"
              class="kylin-login-component-height"
              v-model="loginForm.username"
              prefix-icon="el-icon-user-solid"
              autoComplete="off"
              placeholder='用户名'
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              name="password"
              type="password"
              prefix-icon="el-icon-lock"
              v-model="loginForm.password"
              autoComplete="off"
              placeholder='密码'
              @keyup.enter.native="handleLogin"
              @keyup.native="loginKeyDown"
            ></el-input>
             <div style="height: 15px" v-show="bigChar">
              <el-tag style="margin-left: 20px">{{
                $t("Caps_lock")
              }}</el-tag>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              style="width: 100%"
              @click.native.prevent="handleLogin"
              >{{$t('login.submit')}}</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="login-page-footer">
        <p>麒麟软件有限公司 | 联系电话：400-089-1870</p>
    </div>
  </div>
</template>

<script>
import logoHeader from "@/assets/images/login_logo.png";
import logoContent from "@/assets/images/login_pg_img.png";
import {encrypt} from "@/utils/crypto";
export default {
  data() {
    return {
      bigChar:false,
      logoHeader,
      logoContent,
      loading: false,
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "change", message: this.$t("login.username_required") }],
        password: [{ required: true, trigger: "change", message: this.$t("login.password_required") }],
      },
    };
  },

  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
            this.loading = true;
            let data = {
                username: this.loginForm.username,
                password: encrypt(this.loginForm.password, this.loginForm.username)
            }
            this.$store.dispatch("LoginByUsername", data).then((res) => {
                this.loading = false;
                this.$router.push({
                    path: '/',
                    query: {
                        page: 1,
                        per_page: 20
                    }
                })
            }).catch(error => {
                this.loading = false;
                this.$message({
                    message: error.message,
                    type: 'error'
                })
            })
        }
      });
    },    loginKeyDown(event) {
      const _this = this;
      //是否输入过字母键，且判断是否按键为caps lock
      if (_this.firstTochar) {
        if (event.keyCode === 20) {
          _this.bigChar = !_this.bigChar;
          return;
        }
      }
      //未输入过字母键，或按键不是caps lock，判断每次最后输入的字符的大小写
      var e = event || window.event;
      var keyvalue = e.keyCode ? e.keyCode : e.which;
      var shifKey = e.shiftKey ? e.shiftKey : keyvalue == 16 ? true : false;
      if (typeof _this.loginForm.password === "undefined") {
        return;
      }
      var strlen = _this.loginForm.password.length;
      var password = _this.loginForm.password;

      if (strlen) {
        var uniCode = password.charCodeAt(strlen - 1);
        if (keyvalue >= 65 && keyvalue <= 90) {
          //如果是字母键
          _this.firstTochar = true;
          if (
            (uniCode >= 65 && uniCode <= 90 && !shifKey) ||
            (uniCode >= 97 && uniCode <= 122 && shifKey)
          ) {
            _this.bigChar = true;
          } else {
            _this.bigChar = false;
          }
        }
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login-page {
  height: 100%;
  position: relative;
  background-color: #232323;
  .login-page-header {
    height: 80px;
    font-size: 16px;
    line-height: 1em;
    background: linear-gradient(to bottom, #202020, #212121);
    position: relative;
    color: #fff;
    .login-page-header-logo {
      margin-left: 20px;
      line-height: 80px;
      float: left;
      img {
        height: 70px;
      }
    }
    .login-page-header-text {
      position: relative;
      top: 50%;
      transform: translateY(-50%);
      p {
          padding: 5px 0;
      }
    }
  }
  .login-page-content {
    width: 600px;
    height: 333px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: flex-start;
    .login-page-content-logo {
      img {
        width: 350px;
      }
    }
    .login-page-content-form {
        margin: 0 auto;
      .login-page-content-form-header {
        height: 120px;
        line-height: 120px;
        h1 {
          color: #009de6;
          font-weight: bold;
          text-align: center;
        }
      }
    }
  }
  .login-page-footer {
      position: absolute;
      left: 0;
      bottom: 0;
      font-size: 12px;
      text-align: center;
      color: #fff;
      width: 100%;
      p {
          text-align: center;
      }
  }
}
</style>